package com.test.camera;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity {
    private static final String LOG_TAG = "Camera";
    private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;
    private Button takePicBtn = null;
    private ImageView imageView = null;
    private Uri fileUri;
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        takePicBtn = (Button) findViewById(R.id.button);
        takePicBtn.setOnClickListener(takePiClickListener);
        imageView = (ImageView) findViewById(R.id.imageView);
    }

    private final OnClickListener takePiClickListener = new View.OnClickListener()
    {

        @Override
        public void onClick(View v)
        {
            // 利用系统自带的相机应用:拍照
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            //得到照片的地址
            fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
            //将拍照后的照片根据fileUri格式进行存储
            intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
            startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
        }

    };

    public static final int MEDIA_TYPE_IMAGE = 1;
  

    private static Uri getOutputMediaFileUri(int type)
    {
        return Uri.fromFile(getOutputMediaFile(type));
    }

     //创建拍照后的照片的名字跟路径
    private static File getOutputMediaFile(int type)
    {
       
        File mediaStorageDir = null;
        try
        {
        	//专门存放照片的目录
           mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
                    "MyCameraApp");

        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

        if (!mediaStorageDir.exists())
        {
            if (!mediaStorageDir.mkdirs())
            {
              
             Log.d(LOG_TAG,"failed to create directory, check if you have the WRITE_EXTERNAL_STORAGE permission");
               return null;
            }
        }
          //给文件命名
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        File mediaFile;
        if (type == MEDIA_TYPE_IMAGE)
        {
            mediaFile = new File(mediaStorageDir.getPath() + File.separator
                    + "IMG_" + timeStamp + ".jpg");   //IMG_20171129_160453.jpg
        }
        else
        {
            return null;
        }

        return mediaFile;
    }
  //拍完照片过后，将照片显示出来
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent)
    {
        super.onActivityResult(requestCode, resultCode, intent);
        // 如果是拍照
        if (CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE == requestCode)
        {
         //如果拍照resultCode=RESULT_OK,就表示拍照成功
            if (RESULT_OK == resultCode)
            {
               
                if (intent != null)
                {
                  
                    if (intent.hasExtra("data"))
                    {
                    	//从相机中取出所拍摄的照片，并转换成位图bitmap
                        Bitmap bitmap = intent.getParcelableExtra("data");
                        //将拍摄的照片在imageView上面显示出来
                        imageView.setImageBitmap(bitmap);
                    }
                }
                else
                {
                	//显示默认照片
                    int width = imageView.getWidth();
                    int height = imageView.getHeight();
                    //图片参数设置
                    BitmapFactory.Options factoryOptions = new BitmapFactory.Options();

                    factoryOptions.inJustDecodeBounds = true;
                    BitmapFactory.decodeFile(fileUri.getPath(), factoryOptions);

                    int imageWidth = factoryOptions.outWidth;
                    int imageHeight = factoryOptions.outHeight;
                    int scaleFactor = Math.min(imageWidth / width, imageHeight
                            / height);
                    factoryOptions.inJustDecodeBounds = false;
                    factoryOptions.inSampleSize = scaleFactor;
                    factoryOptions.inPurgeable = true;
                     //读取位图
                    Bitmap bitmap = BitmapFactory.decodeFile(fileUri.getPath(),
                            factoryOptions);
                    //将默认照片在imageView上面显示出来
                    imageView.setImageBitmap(bitmap);
                }
            }
            else if (resultCode == RESULT_CANCELED)
            {
                // User cancelled the image capture
            }
            else
            {
                // Image capture failed, advise user
            }
        }

    }

}
